#!/bin/bash -x
cd $2;

echo "[INFO] Fazendo download de arquivos contidos em $1 para $2";

resourceType=$(curl $1 -I -X GET | grep 'X-Resource-Type: DIRECTORY')

if [ ${#resourceType} -gt 0 ]; then 
   for k in $(curl $1) ; do 
      dir=$(echo $k | sed "s/^\(.*\)\/\+[^\/]\+$/\1/")	
      mkdir -p ./$dir
      curl $1/$k  -o ./$k
   done 
else
   filename=$(echo $1 | sed "s/^.*\/\+\([^\/]\+\)$/\1/")
   curl --data-binary $1  > $filename
fi


